<?php

/**
 * 
 *
 * @version $Id: user_man.inc.php,v 1.7 2005/01/05 13:36:39 phppc Exp $
 * @copyright 2004 
 **/

$this->uf_field[1] = "user_name";
	$this->uf_input[1]["size"] = 20;
	$this->uf_input[1]["ml"] = 40;
$this->uf_field[2] = "email";
	$this->uf_input[2]["size"] = 20;
	$this->uf_input[2]["ml"] = 50;
$this->uf_field[3] = "ncp_id";
	$this->uf_input[3]["type"] = "input_db";
	$this->uf_input[3]["abfrage"] = "SELECT id,name FROM ".$this->config["prefix"]."perm";
	$this->uf_input[3]["id"] = "id";
	$this->uf_input[3]["name"] = "name";
$this->uf_field[4] = "pw";
	$this->uf_input[4]["type"] = "password";
$this->uf_field[5] = "pw_conf";
	$this->uf_input[5]["type"] = "password";
$this->uf_field[6] = "random_pw";
	$this->uf_input[6]["type"] = "yes_no";
$perms_array = array();
$user_loop = "";
if($this->perm_array["user_new"] == 1) array_push($perms_array,"create_user");
if($this->perm_array["user_edit"] == 1) array_push($perms_array,"edit","do_edit","del");
$user_man = $this->fun_sg2var("user_man");
$user_man = $this->uf_form_conf_def($user_man,$perms_array,"show");
$this->module_new_value["action"] = "user_man";
if ($user_man == "show") {
	$abfrage = "SELECT ".$this->config["db_table_user"].".id AS id,".$this->config["db_table_user"].".user_name AS user_name,".$this->config["db_table_user"].".email AS email,".$this->config["db_table_user"].".ncp_id AS ncp_id,".$this->config["prefix"]."perm.name AS perm_name FROM ".$this->config["db_table_user"]." LEFT JOIN ".$this->config["prefix"]."perm ON ".$this->config["db_table_user"].".ncp_id = ".$this->config["prefix"]."perm.id";
	if (isset($_POST["show"]) AND $_POST["show"] == "search") {
		if ($_POST["search_name"] != "") $_POST["search_name"] = str_replace("*","%",$_POST["search_name"]);
		if ($_POST["search_name"] != "" AND $_POST["perm_id"] != "0") $abfrage .= " WHERE ".$this->config["db_table_user"].".user_name LIKE \"$_POST[search_name]\" AND ".$this->config["prefix"]."perm.id = \"$_POST[perm_id]\"";
		elseif ($_POST["search_name"] != "" OR $_POST["perm_id"] != "0") $abfrage .= " WHERE ".$this->config["db_table_user"].".user_name LIKE \"$_POST[search_name]\" OR ".$this->config["prefix"]."perm.id = \"$_POST[perm_id]\"";
	}
    $erg = $this->db_abfrage_q ($abfrage);
	while($db_user = $this->db_abfrage_fetch($erg)){
		$this->tpl_load("user_man_user","admin/");
		$edit_link = $this->fun_make_link("admin.php","user_man=edit&u_id=".$db_user["id"]."");
		$this->tpl_replace_vars(array("user_name" => $db_user["user_name"],"email" => $db_user["email"],"perm_name" => $db_user["perm_name"],"edit_link" => $edit_link,"user_id" => $db_user["id"]));
		$user_loop .= $this->tpl_output();
	} // while
	$hidden_array["action"] = "user_man";
	$hidden_array["user_man"] = "create_user";
	if ($this->perm_array["user_new"] == 1) {
		$var_array["ncp_id"] = $this->db_config["standard_perm"];
		$var_array["random_pw"] = 1;
		$create_user_form = $this->uf_create_complete_form("admin_user_create_user_title",$hidden_array,"admin_user_create_user","form_reset","complete_form","uf/","admin.php",$var_array,"admin_user_");
	}
	unset($hidden_array);
	$hidden_array["user_man"] = "del";
	$hidden_form = $this->uf_create_hidden_form($hidden_array);
	$this->tpl_load("user_man","admin/");
	$this->module_new_value["action"] = "perm";
	$link2perm = $this->fun_make_link("admin.php","");
	$this->module_new_value["action"] = "user_man";
	$this->tpl_replace_vars(array("hidden_form" => $hidden_form,"link2perm" => $link2perm,"user_loop" => $user_loop,"create_user_form" => $create_user_form));
	$nc_main = $this->tpl_output();
	$perm_search_input = $this->uf_make_input_db("SELECT id,name FROM ".$this->config["prefix"]."perm",0,"id","name","perm_id",3,0,"admin_user_man_search_no_perm");

	$help_id = 5;
	$stat["admin_stat_anzahl_user"] = $this->db_anzahl_zeilen("SELECT id FROM ".$this->config["db_table_user"]."");
	$stat["admin_stat_anzahl_user_aktuell"] = $this->db_anzahl_zeilen($abfrage);
}elseif ($user_man == "create_user") {
	list($result,$act_key) = $this->session_user("create_user",$_POST["ncp_id"]);
	if ($result == "new") {
		$this->message_green[] = 6;
		$this->w_leitung["url"] = $this->fun_make_link("admin.php");
	}elseif($result == "invalid_input") $this->message_red[] = 1;
	elseif($result == "invalid_email") $this->message_red[] = 3;
	elseif($result == "pw_error") $this->message_red[] = 2;
	
	$this->w_leitung["number"] = 1;
}elseif ($user_man == "edit") {
	$var_array = $this->db_abfrage_az("SELECT user_name,email,ncp_id FROM ".$this->config["db_table_user"]." WHERE id=\"".$_GET["u_id"]."\"");
	$hidden_array["action"] = "user_man";
	$hidden_array["user_man"] = "do_edit";
	$hidden_array["u_id"] = $_GET["u_id"];
	$this->v_uname = $var_array["user_name"];
	$nc_main = $this->uf_create_complete_form("admin_user_edit_user_title",$hidden_array,"admin_user_edit_user","form_reset","complete_form","uf/","admin.php",$var_array,"admin_user_");
	$help_id = 6;
}elseif ($user_man == "do_edit") {
	$_POST = $this->uf_secure($_POST);
	list($result,$user_name) = $this->session_user("do_edit",$_POST["ncp_id"],$_POST["u_id"]);
	if ($result == "edit") {
		$this->message_green[] = 7;
		$this->w_leitung["number"] = 3;
		$this->w_leitung["url"] = $this->fun_make_link("admin.php");
	}else {
		if($result == "invalid_input") $this->message_red[] = 1;
		elseif($result == "invalid_email") $this->message_red[] = 3;
		elseif($result == "pw_error") $this->message_red[] = 2;
		$this->w_leitung["number"] = 1;
	}
}elseif ($user_man == "del") {
	if (isset($_POST["del_user"])) {
		$anzahl_del_user = 0;
		$anzahl_del_user_insgesamt = 0;
	    foreach ($_POST["del_user"] as $user_id) {
			$anzahl_user_id = $this->db_anzahl_zeilen ("SELECT id FROM ".$this->config["db_table_user"]." WHERE id = \"$user_id\"");
			if ($anzahl_user_id == 1) {
			    list ($ncp_id) = $this->db_abfrage("SELECT ncp_id FROM ".$this->config["db_table_user"]." WHERE id = \"$user_id\"");
				list ($valid_del) = $this->db_abfrage("SELECT del FROM ".$this->config["prefix"]."perm WHERE id = \"$ncp_id\"");
				if ($valid_del == 1) {
				    $this->db_abfrage_q ("DELETE FROM ".$this->config["db_table_user"]." WHERE id = \"$user_id\"");
					$anzahl_del_user++;
				}
			}
			$anzahl_del_user_insgesamt++;
		}
		if ($anzahl_del_user == 0) $this->message_red[] = 5;
		else $this->message_green[] = 8;
	}else $this->message_red[] = 4;
	$this->w_leitung["number"] = 3;
	$this->w_leitung["url"] = $this->fun_make_link("admin.php","user_man=show"); 

}

?>